package com.woniuxy.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.commons.core.model.Result;
import com.woniuxy.user.entity.UserAddresses;
import com.woniuxy.user.form.AddAddressesForm;
import com.woniuxy.user.param.AddAddressesParam;
import com.woniuxy.user.service.IUserAddressesService;
import com.woniuxy.user.service.IUserLogService;
import com.woniuxy.user.utils.GaoDeMapUtil;
import com.woniuxy.user.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/user-addresses")
@Api(tags = "用户地址管理")
public class UserAddressesController {
    @Resource
    private IUserAddressesService userAddressesService;
    @Resource
    private IUserLogService userLogService;

    /**
     * 用户添加地址
     */
    @PostMapping("/addAddresses")
    @ApiOperation(value = "用户新增地址信息")
    public Result<String> addAddresses(@RequestBody AddAddressesForm addAddressesForm, @RequestHeader("token") String token) throws Exception {
        int userId = Integer.parseInt(JwtUtils.parseToken(token).get("id").toString());
        addAddressesForm.setUserId(Integer.parseInt(String.valueOf(userId)));
        userAddressesService.addAddresses(BeanUtil.toBean(addAddressesForm, AddAddressesParam.class));
        return new Result<String>().setMsg("新增地址成功").setCode(200);

    }



    /**
     * 用户删除地址信息
     *
     */
    @DeleteMapping("/deleteAddresses")
    @ApiOperation(value = "用户删除地址信息")
    public Result<String> deleteAddresses(@RequestHeader("token") String token, int id)  throws Exception{
        // 从 token 中拿到用户 id
        String userIdStr = JwtUtils.parseToken(token).get("id").toString();
        Integer userId = Integer.parseInt(userIdStr);

        // 调用服务层方法删除地址，返回布尔值表示操作是否成功
        boolean deleteSuccess = userAddressesService.deleteAddresses(userIdStr, id);
        String actionDesc = "用户删除地址信息，地址ID：" + id;
        Integer successFlag = deleteSuccess ? 1 : 0;
        String errorMsg = deleteSuccess ? null : "地址删除失败";

        // 根据操作结果记录日志
        userLogService.logDeleteAddressAction(String.valueOf(userId), actionDesc, successFlag, errorMsg);
        System.out.println("日志记录结果：" + successFlag);

        if (deleteSuccess) {
            return new Result<String>().setMsg("删除地址成功").setCode(200);
        } else {
            return new Result<String>().setMsg("删除地址失败").setCode(400);
        }


    }

    /**
     * 用户查看自己的地址信息
     */
    @GetMapping("/getAddresses")
    @ApiOperation(value = "用户查看自己的地址信息")
    public Result<List<UserAddresses>> getAddresses(int pageNo, int pageSize,@RequestHeader("token") String token) throws Exception  {
        // 从 token 中拿到用户 id
        String userId = JwtUtils.parseToken(token).get("id").toString();
        // 调用服务层方法查询所有地址信息
        List<UserAddresses> addressesList = userAddressesService.getAllAddresses(userId);

        return new Result<List<UserAddresses>>().setCode(200).setMsg("查询自己地址信息成功").setData(addressesList);

    }

    /**
     * 用户设置默认地址
     */
    @PutMapping("/setDefaultAddress")
    @ApiOperation(value = "用户设置默认地址")
    public Result<String> setDefaultAddress(@RequestHeader("token") String token, int id) throws Exception {
        // 从 token 中拿到用户 id
        String userId = JwtUtils.parseToken(token).get("id").toString();
        // 调用服务层方法设置默认地址
         userAddressesService.setDefaultAddress(userId, id);
        return new Result<String>().setMsg("设置默认地址成功").setCode(200);
    }


}
